package com.njwb.day14.dao.swing;

import com.njwb.day14.dao.pojo.Student;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.sql.SQLException;
import java.util.EventObject;

// 封装按钮面板
class ButtonPanel extends JPanel {
    JButton deleteButton = new JButton("删除");
    JButton editButton = new JButton("修改");

    public ButtonPanel() {
        setLayout(new FlowLayout(FlowLayout.CENTER, 8, 0));
        add(deleteButton);
        add(editButton);
    }
}

// 渲染器
class ButtonRenderer extends ButtonPanel implements TableCellRenderer {
    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
                                                   boolean hasFocus, int row, int column) {
        return this;
    }
}

// 编辑器
class ButtonEditor extends AbstractCellEditor implements TableCellEditor {
    private final ButtonPanel panel;
    private final JTable table;
    private final DefaultTableModel model;
    private final StudentService service;

    public ButtonEditor(JTable table, DefaultTableModel model, StudentService service) {
        this.table = table;
        this.model = model;
        this.service = service;
        panel = new ButtonPanel();

        // 删除
        panel.deleteButton.addActionListener(this::deleteAction);
        // 修改
        panel.editButton.addActionListener(this::editAction);
    }

    private void deleteAction(ActionEvent e) {
        int row = table.getEditingRow();
        if (row < 0) return; // 避免非法行

        int sid = (int) model.getValueAt(row, 0);
        int confirm = JOptionPane.showConfirmDialog(null,
                "确定要删除学号为 " + sid + " 的学生吗？", "确认删除", JOptionPane.YES_NO_OPTION);
        if (confirm == JOptionPane.YES_OPTION) {
            try {
                if (service.deleteStudent(sid) > 0) {
                    // 停止编辑再删除
                    if (table.isEditing()) {
                        table.getCellEditor().stopCellEditing();
                    }
                    model.removeRow(row);
                    JOptionPane.showMessageDialog(null, "删除成功！");
                } else {
                    JOptionPane.showMessageDialog(null, "删除失败！");
                }
            } catch (SQLException ex) {
                ex.printStackTrace();
                JOptionPane.showMessageDialog(null, "数据库错误！");
            }
        } else {
            fireEditingStopped();
        }
    }


    private void editAction(ActionEvent e) {
        int row = table.getEditingRow();
        int sid = (int) model.getValueAt(row, 0);
        String oldName = (String) model.getValueAt(row, 1);
        int oldAge = (int) model.getValueAt(row, 2);
        String oldAddress = (String) model.getValueAt(row, 3);

        String newName = JOptionPane.showInputDialog(null, "修改姓名：", oldName);
        if (newName == null) { fireEditingStopped(); return; }

        String ageStr = JOptionPane.showInputDialog(null, "修改年龄：", oldAge);
        if (ageStr == null || !ageStr.matches("\\d+")) {
            JOptionPane.showMessageDialog(null, "年龄输入无效！");
            fireEditingStopped();
            return;
        }

        String newAddress = JOptionPane.showInputDialog(null, "修改地址：", oldAddress);
        if (newAddress == null) { fireEditingStopped(); return; }

        try {
            Student stu = new Student(sid, newName, Integer.parseInt(ageStr), newAddress);
            if (service.updateStudent(stu) > 0) {
                model.setValueAt(newName, row, 1);
                model.setValueAt(Integer.parseInt(ageStr), row, 2);
                model.setValueAt(newAddress, row, 3);
                JOptionPane.showMessageDialog(null, "修改成功！");
            } else {
                JOptionPane.showMessageDialog(null, "修改失败！");
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
            JOptionPane.showMessageDialog(null, "数据库错误！");
        }
        fireEditingStopped();
    }

    @Override
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
        return panel;
    }

    @Override
    public Object getCellEditorValue() { return null; }

    @Override
    public boolean isCellEditable(EventObject e) { return true; }
}
